package com.shangyueshang.controller.basic;

import com.shangyueshang.model.SysDept;
import com.shangyueshang.request.IdDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.service.SysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 18:41
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private SysDeptService sysDeptService;

    /**
     * 保存机构
     *
     * @param sysDept
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('basic:dept:add') and hasAuthority('basic:dept:edit')")
    public HttpResult save(@Valid @RequestBody SysDept sysDept) {
        return HttpResult.ok(sysDeptService.saveUpdate(sysDept));
    }

    /**
     * 删除机构
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('basic:dept:delete')")
    public HttpResult delete(@NotEmpty @RequestBody List<IdDTO> ids) {
        return HttpResult.ok(sysDeptService.removeByIds(ids.stream().map(IdDTO::getId).collect(Collectors.toList())));
    }

    /**
     * 查询机构树
     *
     * @return
     */
    @GetMapping("/findTree")
    @PreAuthorize("hasAuthority('basic:dept:view')")
    public HttpResult findTree() {
        return HttpResult.ok(sysDeptService.findTree());
    }
}
